package com.zerdoor.controller;

import com.zerdoor.model.UserInfo;
import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.AribitrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 仲裁
 * Created by xiaoji on 2015/1/7.
 */
@Controller
@RequestMapping("/arbitration")
public class ArbitrationController {

    @Autowired
    private AribitrationService aribitrationService;

    @RequestMapping("toArtistIndex")
    public String toArtistIndex(){
        return "/arbitration/artistIndex";
    }

    @RequestMapping("toPrincipalIndex")
    public String toPrincipalIndex(){
        return "/arbitration/principalIndex";
    }

    /**
     * FunName:           getArtistAribitration
     * Description :      会员中心-艺术家我的仲裁
     * @return String：艺术家仲裁数据
     * @Author:       xiaoji
     * @Create Date: 2015-1-7
     */
    @RequestMapping(value = "/getArtistAribitration", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getArtistAribitration(HttpServletRequest request,int page,int rows){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        Map<String, Object> map = new HashMap<String,Object>();
        PageHelper.startPage(page, rows);
        List<Map<String, Object>> list = aribitrationService.getArtistAribitration(userInfo.getArtistId());
        PageInfo pageInfo = new PageInfo(list);
        map.put("pageInfo",pageInfo);
        return map;
    }

    /**
     * FunName:           getPrincipalAribitration
     * Description :      会员中心-委托方我的仲裁
     * @return String：艺术家仲裁数据
     * @Author:       xiaoji
     * @Create Date: 2015-1-7
     */
    @RequestMapping(value = "/getPrincipalAribitration", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getPrincipalAribitration(HttpServletRequest request,int page,int rows){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        Map<String, Object> map = new HashMap<String,Object>();
        PageHelper.startPage(page, rows);
        List<Map<String, Object>> list = aribitrationService.getPrincipalAribitration(userInfo.getBaileId());
        PageInfo pageInfo = new PageInfo(list);
        map.put("pageInfo",pageInfo);
        return map;
    }

    /**
     * 仲裁详情页面
     * @param contractId 合同号
     * @param request 请求
     * @return 返回仲裁信息
     */
    @RequestMapping(value = "/arbitrationDetail", method = RequestMethod.GET)
    public ModelAndView arbitrationDetail(int contractId,HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        String arbitrateId = request.getParameter("arbitrateId");
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userInfo.getUserId());
        map.put("arbitrateId",arbitrateId);
        map.put("baileId",userInfo.getBaileId());
        map.put("artistId",userInfo.getArtistId());

        if(arbitrateId != null){//查询仲裁论述记录
            Map<String,Object> detail = aribitrationService.getAribitrationById(map);
            if(detail != null){
                modelAndView.addObject("discuss",aribitrationService.getItemDiscuss(map));
                modelAndView.addObject("detail",detail);
            }

        }else{//申请仲裁时候只显示项目相关信息
            map.put("contractId",contractId);
            Map<String,Object> detail = aribitrationService.getContractById(map);
            detail.put("applyTime", new Date());
            modelAndView.addObject("detail",detail);
        }
        modelAndView.addObject("contractId", contractId);
        modelAndView.addObject("arbitrateId", arbitrateId);
        modelAndView.setViewName("/arbitration/arbitrationDetail");
        return modelAndView;
    }
    @RequestMapping(value = "/addItemDiscuss", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addItemDiscuss(HttpServletRequest request){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("userId", userInfo.getUserId());
        map.put("content", request.getParameter("content"));
        map.put("attachment", request.getParameter("attachment"));

        try {
            String arbitrateId = request.getParameter("arbitrateId");
            if(arbitrateId == ""){//第一次发布描述时添加申请仲裁
                map.put("arbitrateId", null);
                map.put("contractId", Integer.parseInt(request.getParameter("contractId")));
                map.put("applyReason",request.getParameter("content"));
                aribitrationService.addArbitration(map);
                map.put("arbitrateId", map.get("arbitrate_id"));//获取仲裁编号
            }else{
                map.put("arbitrateId", arbitrateId);
            }

            aribitrationService.addItemDiscuss(map);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("errormsg","发布描述失败！");
        }
        return map;
    }
}
